<?php

namespace App\Http\Controllers\Api;

use App\Libs\WeChat\WxSmallClient;
use App\Models\e_user;
use App\Models\e_user_info_likes;
use App\Models\e_user_note_info;
use App\Models\like_count;
use App\Models\punch_activity;
use App\Models\punch_comments;
use App\Models\punch_relations;
use App\Models\user;
use App\Models\user_info_likes;
use App\Models\user_punch_record;
use App\Models\user_share_info;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redis;

class UserController extends Controller
{
    /**
     * 上传图片
     *
     */
    public function upload(){
        echo 123;
        $avatar = (isset($_FILES['image']) && !empty($_FILES['image'])) ?$_FILES['image'] :'';
        var_dump($avatar);
        if( empty($avatar) )
        {
            fun_respon(0,'缺少参数！');
        }
        if( $avatar['error'] != 0 || $avatar['size'] <= 0 )
        {
            fun_respon(0,'数据不能为空！');
        }

        $uploadclass = new UploadController();
        $img = $uploadclass->iosuploadimg($avatar);
        if( isset($img['code']) && !empty($img['code']))
        {
            $success = array(
                'url'=>$img['data']
            );
            fun_respon(1,$success);
        }
        fun_respon(0,'图片转移失败！');
    }

    /**
     * regist 注册，以及获取token凭证接口
     */
    public function regist(Request $request)
    {
        $code = $request->json('code');
        $iv = $request->json('iv');
        $cryptData = $request->json('cryptData');
        //$activity = (int) $request->json('activity', 0); // 活动进入用户标记

        if (empty($code) || empty($iv) || empty($cryptData)) {
            fun_respon(0, '缺少参数');
        }
        $rs = WxSmallClient::getSessionKey($code);
        $array_user = json_decode($rs);
        if (!is_object($array_user)) {
            fun_respon(0, '网络异常请重试');
        }
        if ( !property_exists($array_user, 'session_key')) {
            fun_respon(0, '解码失败');
        }


        $userDatas = WxSmallClient::decryptData($array_user->session_key, $iv, $cryptData);
        $userData = json_decode($userDatas, true);

        if (is_array($userData) && !empty($userData)) {
            // 去添加用户
            $openid = $userData['openId'];
            $unionid = isset($userData['unionId'])?$userData['unionId']:'';
            $uinfo = e_user::where('openid', $userData['openId'])->first();
            if (!$uinfo) {
                $add_data = [
                    'openid' => $openid,
                    'unionid' => $unionid,
                    'gender' => $userData['gender'],
                    'avatarUrl' => $userData['avatarUrl'],
                    'nickName' => $userData['nickName'],
                ];
                $res = e_user::insertGetId($add_data);
                $uinfo = e_user::where('id', $res)->first();
            }else{
                if($unionid){
                    $res = e_user::where('openid', $userData['openId'])->update(['unionid' => $userData['unionId']]);
                    if(!$res) fun_respon(0, '更新数据错误');
                }
            }
            $token = Crypt::encrypt(['openid'=>$openid]);
            Redis::setex('experience_token_'.$openid, 14400, $token);
            Redis::setex('Experience_user_info'.$openid, 14400, json_encode($uinfo->toArray()));
            fun_respon(1, ['token'=>$token,"info"=>$uinfo->toArray()]);
        }
        fun_respon(0, '数据错误');
//        $code = $request->json('code');
//        if (empty($code)) fun_respon(0, '缺少参数');
//
//        $rs = WxSmallClient::getSessionKey($code);
//        $array_user = json_decode($rs);
//        if (!is_object($array_user)) {
//            fun_respon(0, '网络异常请重试');
//        }
//        if ( !property_exists($array_user, 'session_key')) {
//            fun_respon(0, '解码失败');
//        }
//
//        $openid = $array_user->openid;
//        $user = user::where('openid', $openid)->first();
//        if (!empty($user)) {
//            $token = Crypt::encrypt(['openid'=>$user['openid']]);
//            Redis::setex('punch_token_'.$openid, 14400, $token);
//            fun_respon(1, ['token'=>$token, 'info'=>$user->toArray()]);
//        }
//        $add_data = [
//            'openid' => $openid
//        ];
//        if (property_exists($array_user, 'unionid')) {
//            $add_data['unionid'] = $array_user->unionid;
//        }
//        $res = user::insertGetId($add_data);
//        $token = Crypt::encrypt(['openid'=>$openid]);
//        Redis::setex('punch_token_'.$openid, 14400, $token);
//        $uinfo = user::where('id', $res)->first();
//        if ($res) fun_respon(1, ['token'=>$token,"info"=>$uinfo->toArray()]);
//        fun_respon(0, '注册失败');

    }


    /**
     * 用户注册 获取token
     * @param Request $request
     */
    public function usersregist(Request $request)
    {
        $openid = $request->json('openid');
        if (empty($openid)) fun_respon(0, '缺少参数');
        $user = user::where('openid', $openid)->first();
        $token = Crypt::encrypt(['openid'=>$openid]);
        if (!empty($user)) {
            Redis::setex('punch_token_'.$openid, 14400, $token);
            fun_respon(1, ['token'=>$token, 'info'=>$user->toArray()]);
        }

        $res = user::insert(['openid'=>$openid]);
        $token = Crypt::encrypt(['openid'=>$openid]);
        if ($res) {
            Redis::setex('punch_punch_token_'.$openid, 14400, $token);
            fun_respon(1, ['token'=>$token]);
        }
        fun_respon(0, '注册失败');
    }

    /**
     * 获取用户信息
     */
    public function userinfo(Request $request)
    {
        $openid = $request->openid;
        $userinfo = user::where('openid', $openid)->first();
        if (!$userinfo) fun_respon(0, '用户未注册');
        fun_respon(1, $userinfo->toArray());
    }

    /**
     * 修改用户信息
     */
    public function edituser(Request $request)
    {
        $openid = $request->openid;
        $nickname = $request->json('nickName');
        $header_url = $request->json('avatarUrl');
        $constellation = $request->json('constellation');
        $age = $request->json('age');
        $phone = $request->json('phone');
        $sex = $request->json('gender');

        $params = [
            'nickName' => $nickname,
            'avatarUrl' => $header_url,
            'constellation' => $constellation,
            'age' => $age,
            'phone' => $phone,
            'gender' => $sex,
            'ut' => date('Y-m-d H:i:s'),
        ];
        $res = user::where('openid', $openid)->update($params);
        if ($res) fun_respon(1, '修改成功');
        fun_respon(1, '修改失败');
    }

    /**
     * 收集用户form_id
     */
    public function saveformid(Request $request)
    {
        try {
            $openid = $request->openid;
            $form_id = $request->json('form_id');
            $data = ['openid'=>$openid, 'form_id'=>$form_id];
            $res = user_formid::insert($data);
            fun_respon(1, '录入成功');
        } catch (\Exception $ex) {
            var_dump($ex->getMessage().$ex->getLine());
            fun_respon(0, '系统繁忙，请稍后再试！');
        }
    }



    /**
     * @param Request $request
     * 个人页面
     */
    public function getUserInfo(Request $request){
        $openid = $request->openid;
        $data = e_user_note_info::leftJoin('e_user_class_record','e_user_note_info.id','=','e_user_class_record.e_user_note_id')->leftJoin('e_class','e_class.id','=','e_user_class_record.e_class_id')->where(['e_user_note_info.user_id'=>$openid])->get(['e_class.title','e_user_note_info.content','e_user_note_info.created_at','e_user_class_record.e_class_id','e_user_class_record.e_user_note_id']);
        if(!$data){
            fun_respon(0, '暂无数据');
        }
        foreach ($data as &$v){
            //查询点赞数量
            $where = ['user_note_info_id'=>$v['e_user_note_id'],'class_id'=>$v['e_class_id'],'status'=>1];
            $like_num = e_user_info_likes::where($where)->count();
            $v['like_num'] = $like_num?$like_num:0;
        }
        $datas['note_list'] = $data;
        $datas['note_num'] = count($data);;
        fun_respon(1, $datas);
    }

    //获取参与人数
    private function joinnum($punch_id){
        $join_punch = user_punch_record::leftJoin('user','user_punch_record.user_id','=','user.openid')
            ->where(['user_punch_record.punch_ac_id'=>$punch_id,'user_punch_record.is_valid'=>1])->groupBy(['user_punch_record.user_id'])
            ->get(['avatarUrl']);
        return $join_punch;
    }

    /**
     * 列表主页
     * @param Request $request
     */
    public function getPunchList(Request $request){
        $openid = $request->openid;
        $type = $request->json('type');
        if(!in_array($type,[1,2,3])){
            fun_respon(0, '参数错误');
        }
        if($type==1){
            $where = ['user_punch_record.user_id'=>$openid,'punch_activity.status'=>1,'user_punch_record.is_del'=>0];
        }elseif ($type==2){
            $where = ['user_punch_record.user_id'=>$openid,'punch_activity.status'=>2,'user_punch_record.is_del'=>0];
        }elseif ($type==3){
            $where = ['punch_activity.user_id'=>$openid,'punch_activity.is_del'=>0];
        }
        $punch_info = $this->getPunch($where);
        if($punch_info){
            foreach ($punch_info as $key=>$v){
                $joiner = $this->joinnum($v['punch_id']);
                $punch_info[$key]['join_num'] = $joiner?count($joiner):0;

                $where = ['user_id'=>$openid,'punch_ac_id'=>$v['punch_id']];
                //查询打卡的天数
                $my_punch_num =user_punch_record::where($where)->count();
                $punch_info[$key]['my_punch_num'] = $my_punch_num;
                //$count_num = user_punch_record::where(['punch_ac_id'=>$v['punch_ac_id']])->count();
                //查询用户这个活动今天是否打卡
                $punch_info[$key]['is_punch'] = $this->getIsPunch($where);
            }
            fun_respon(1, $punch_info);
        }
        fun_respon(0, '暂无数据');
    }



    /**
     * @param $where
     * @return int
     * 查询是否打卡
     */
    private function getIsPunch($where){
        $res = user_punch_record::whereDate('created_at','=',date('Y-m-d'))->where($where)->first();
        return $res = $res?1:0;
    }

    /**
     * 统计数量
     * @param $where
     * @return int
     */
    private function getPunchNum($where){
        $num = punch_activity::leftjoin('user_punch_record', "user_punch_record.punch_ac_id", "=", "punch_activity.id")->where($where)->groupBy(['punch_activity.id'])->get();
        return ($num);
    }

    /**
     * 查询列表
     * @param $where
     * @return int
     */
    private function getPunch($where){
        $data = punch_activity::leftjoin('user_punch_record', "user_punch_record.punch_ac_id", "=", "punch_activity.id")->where($where)->groupBy(['punch_activity.id'])->get(['punch_activity.id as punch_id','punch_activity.user_id','title','content','punch_image','status','join_num','punch_user_info_id','user_punch_record.created_at'])->toArray();
        return $data;
    }

    /**
     * @param Request $request
     * 用户对于某个打卡的打卡记录
     */
    public function myPunchList(Request $request){
        $openid = $request->openid;
        $punch_id = $request->json('punch_id');
        if(!$punch_id) fun_respon(0,'缺少参数');
        $share_info = user_punch_record::leftjoin('user_share_info', "user_punch_record.punch_user_info_id", "=", "user_share_info.id")->leftJoin('user','user_share_info.user_id','=','user.openid')
            ->where(['user_punch_record.punch_ac_id'=>$punch_id,'user_punch_record.is_valid'=>1,'user_punch_record.user_id'=>$openid])
            ->get(['share_image','content','address','punch_ac_id as punch_id','openid','nickName','avatarUrl','user_share_info.created_at','user_share_info.id as user_info_id']);
        if($share_info){
            foreach ($share_info as &$v){
                //查询是否点赞
                $where = ['user_id'=>$openid,'user_share_info_id'=>$v['user_info_id'],'punch_id'=>$punch_id,'status'=>1];
                $re = user_info_likes::where($where)->first();
                $v['is_like'] = $re?1:0;
                //查询点赞数量
                $where = ['user_share_info_id'=>$v['user_info_id'],'punch_id'=>$punch_id,'status'=>1];
                $like_num = user_info_likes::where($where)->count();
                $v['like_num'] = $like_num?$like_num:0;
            }
            fun_respon(1, $share_info);
        }
        fun_respon(1, '暂无数据！');
    }


    /*
     * 点赞统计
     * user_info_id 评论id
     * $user_id 用户id
     * */
    public function setLike(Request $request){
        $user_info_id = $request->json('user_note_info_id');
        $punch_id = $request->json('class_id');
        if(!$user_info_id){
            return fun_respon(0,'参数错误');
        }
        $openid = $request->openid;
        $where = ['user_id'=>$openid,'user_note_info_id'=>$user_info_id,'class_id'=>$punch_id];
        $re = e_user_info_likes::where($where)->first();
        //如果没有点赞 则添加点赞数据
        if($re){
            $status = $re['status']==1?0:1;
            $result = e_user_info_likes::where($where)->update(['status'=>$status]);
        }else{
            $data['user_id']=$openid;
            $data['user_note_info_id']=$user_info_id;
            $data['class_id']=$punch_id;
            $data['status']=1;
            $result=e_user_info_likes::create($data);
        }
        if($result){
            fun_respon(1,'点赞成功!');
        }else{
            fun_respon(0,'写入失败');
        }
    }


    public function ad(){
        $user_aver = e_user::orderBy(\DB::raw('RAND()'))
            ->take(6)
            ->get(['avatarUrl']);
        $ad_num = Redis::get('ad_num');
        $ss = $this->get_rand(array('a'=>20,'b'=>20,'c'=>20,'d'=>20,'e'=>20));
        if($ss=='b'){
            Redis::set('ad_num',$ad_num+1);
        }
        $ad_num = Redis::get('ad_num');
        $data['num'] = $ad_num;
        $data['user'] = $user_aver;
        $data['image'] = 'http://file.ucloud.osv.cn/2018/09/03/A8QBVCKkMDWmCEun9pje.png';
        $data['price'] = 500;
        fun_respon(1,$data);
    }



    public function getAFUserList(Request $request){
        $openid = $request->openid;
        //查询用户信息
        //$user_info = user::where(['openid'=>$openid])->first();
        $uid = $request->json('uid');
        $type = $request->json('type'); //1.关注 2.粉丝
        if(!$uid || !in_array($type,[1,2])) fun_respon(0,'缺少参数！');
        $user_info = user::find($uid);
        if(!($user_info)){
            fun_respon(0,'暂无数据!');
        }
        //获取关注的
        if($type==1){
            $res = punch_relations::where(['from_user_id'=>$uid,'rel_type'=>1])->leftJoin('user','punch_relations.to_user_id','=','user.id')->get()->toArray();
        }else{
            $res = punch_relations::where(['to_user_id'=>$uid,'rel_type'=>1])->leftJoin('user','punch_relations.from_user_id','=','user.id')->get()->toArray();
        }
        if(!$res){
            fun_respon(0,'暂无数据！');
        }
        foreach ($res as &$v){
            //查询关注人数
            $attentions = punch_relations::where(['rel_type'=>1,'from_user_id'=>$v['id']])->count();
            //查询粉丝数
            $fans = punch_relations::where(['rel_type'=>1,'to_user_id'=>$v['id']])->count();
            $dynamic_num = $this->getMyDynamic($v['openid']);
            $v['dynamic_num'] = $dynamic_num;
            $v['attentions'] = $attentions;
            $v['fans'] = $fans;
        }
        fun_respon(1,$res);
    }

    /**
     * @param Request $request
     * 添加取消关注
     */
    public function addRelation(Request $request){
        $openid = $request->openid;
        //查询用户信息
        $user_info = user::where(['openid'=>$openid])->first();
        $uid = $request->json('uid');
        $rel_type = $request->json('rel_type'); //1.关注 2.取消关注
        if(!$uid || !in_array($rel_type,[1,2])) fun_respon(0,'缺少参数！');
        if($uid==$user_info->id){
            fun_respon(0,'不能关注自己哦！');
        }
        $arr['from_user_id'] =$user_info->id;
        $arr['to_user_id'] =$uid;
        //先查询下存在不存在关系，如果不存在 则添加，否则是修改
        $relations = punch_relations::where($arr)->first();
        if($relations){
            //$rel_type = $rel_type == 1?2:1;
            $res = punch_relations::where($arr)->update(['rel_type'=>$rel_type]);
        }else{
            $arr['rel_type'] = $rel_type;
            $res = punch_relations::create($arr);
        }
        if($res){
            fun_respon(1,'操作成功!');
        }
        fun_respon(0,'操作失败!');
    }

    /**
     * @param Request $request
     * 获取他人的主页信息
     */
    public function getOtherInfos(Request $request){
        $openid = $request->openid;
        //查询用户信息
        $user = user::where(['openid'=>$openid])->first();
        $uid = $request->json('uid');
        if(!$uid) fun_respon(0,'缺少参数！');
        $user_info = user::find($uid);
        if(!($user_info)){
            fun_respon(0,'暂无数据!');
        }

        $arr['from_user_id'] =$user->id;
        $arr['to_user_id'] =$uid;
        //先查询下存在不存在关系，如果不存在 则添加，否则是修改
        $relations = punch_relations::where($arr)->first();
        $user_info['is_attention'] = 0;
        if($relations){
            if($relations['rel_type']==1) {
                $user_info['is_attention'] = 1;
            }
        }
        //查询关注人数
        $attentions = punch_relations::where(['rel_type'=>1,'from_user_id'=>$uid])->count();
        //查询粉丝数
        $fans = punch_relations::where(['rel_type'=>1,'to_user_id'=>$uid])->count();
        $dynamic_num = $this->getMyDynamic($user_info->openid);
        $user_info['attentions'] = $attentions;
        $user_info['fans'] = $fans;
        $user_info['dynamic_num'] = $dynamic_num;
        fun_respon(1,$user_info);
    }


    private function getMyDynamic($openid){
        //查询我打卡的
        $where = ['punch_activity.user_id'=>$openid,'punch_activity.is_del'=>0];
        $my_num = $this->getPunch($where);
        //我参与的打卡
        $where = ['user_punch_record.user_id'=>$openid,'punch_activity.status'=>1,'user_punch_record.is_del'=>0];
        $public_result = $this->getPunchLists($where);
        //查询我发布的动态
        $where = ['user_id'=>$openid,'is_valid'=>1];
        $share_num = $this->getUserShareInfos($where);
        $dynamic_num = count($my_num)+count($share_num)+count($public_result);
        return $dynamic_num;
    }


    /**
     * @param Request $request
     * 获取我的圈子
     */
    public function getOtherPunch(Request $request){
        $openid = $request->openid;
        $uid = $request->json('uid');
        if(!$uid) fun_respon(0,'缺少参数！');
        $user_info = user::find($uid);
        if(!($user_info)){
            fun_respon(0,'暂无数据!');
        }
        $public_result = [];
        $my_result = [];
        $where = ['user_punch_record.user_id'=>$user_info['openid'],'punch_activity.status'=>1,'user_punch_record.is_del'=>0];
        $public_punch = $this->getPunch($where);
        if($public_punch){
            $id = array_column($public_punch, 'punch_id');
            $public_result = array_combine($id, $public_punch);
        }
        //我创建的打卡
        $where = ['punch_activity.user_id'=>$user_info['openid'],'punch_activity.is_del'=>0];
        $my_punch = $this->getPunch($where);
        if($my_punch){
            $ids = array_column($my_punch, 'punch_id');
            $my_result = array_combine($ids, $my_punch);
        }
        //合并数组
        $result = array_merge($my_result,$public_result);
        foreach ($result as &$v){
            if($v['user_id']==$user_info->openid){
                $v['is_self'] = 1;
            }else{
                $v['is_self'] = 0;
            }
            unset($v['user_id']);
        }
        fun_respon(1,$result);
    }


    /**
     * @param Request $request
     * 获取我的动态
     */
    public function getOtherDynamic(Request $request){
        $openid = $request->openid;
        $uid = $request->json('uid');
        if(!$uid) fun_respon(0,'缺少参数！');
        $user_info = user::find($uid);
        if(!($user_info)){
            fun_respon(0,'暂无数据!');
        }
        //查看下集合 看是否有数据
        //$res = Redis::zRevRange('user_info'.$uid, 0, -1); // array(val0, val1, val5) WITHSCORES
        $res = 0;
        if($res){
            foreach ($res as &$items){
                $items = json_decode($items,true);
            }
            fun_respon(1,$res);
        }else{
            //查询用户的打卡列表
            $share_info = user_punch_record::leftjoin('user_share_info', "user_punch_record.punch_user_info_id", "=", "user_share_info.id")->leftJoin('punch_activity','user_punch_record.punch_ac_id','=','punch_activity.id')->leftJoin('user','user_share_info.user_id','=','user.openid')
                ->where(['user_punch_record.user_id'=>$user_info['openid'],'user_punch_record.is_valid'=>1])->orderBy('user_punch_record.created_at',"DESC")
                ->get(['share_image','user_share_info.content','address','punch_ac_id as punch_id','openid','nickName','avatarUrl','user_share_info.created_at','user_share_info.id as user_info_id','user_share_info.user_id as uid','punch_activity.title'])->toArray();
            if($share_info){
                foreach ($share_info as &$v){
                    $v['type'] =2 ;
                    //查询自己是否点赞
                    $where = ['user_id'=>$openid,'user_share_info_id'=>$v['user_info_id'],'user_id'=>$openid,'status'=>1];
                    $re = user_info_likes::where($where)->first();
                    $v['is_like'] = $re?1:0;
                    //查询点赞数量
                    $where = ['user_share_info_id'=>$v['user_info_id'],'status'=>1];
                    $like_num = user_info_likes::where($where)->count();
                    $v['like_num'] = $like_num?$like_num:0;
                    //评论列表
                    $punch_comments = punch_comments::where(['share_info_id'=>$v['user_info_id']])->orderBy('created_at','ASC')->get();
                    if($punch_comments){
                        foreach ($punch_comments as &$value){
                            if($value['to_user_id']){
                                $to_user_nickName = user::where(['id'=>$value['to_user_id']])->first(['nickName']);
                                $value['to_user_nickName'] = $to_user_nickName['nickName'];
                            }else{
                                $value['to_user_nickName'] = '';
                            }
                            $value['is_mine'] = $value['user_id']==$openid?1:0;
                            $user_nickName = user::where(['openid'=>$value['user_id']])->first(['nickName','id']);
                            $value['user_nickName'] = $user_nickName['nickName'];
                            $value['user_id'] = $user_nickName['id'];
                            unset($value['to_user_id']);
                            unset($value['share_info_id']);
                            unset($value['punch_id']);
                        }
                    }
                    Redis::zadd('user_info'.$uid,strtotime($v['created_at']),json_encode($v));
                    $v['comments'] = $punch_comments;
                }

            }
            //我参与的公开打卡
            $where = ['user_punch_record.user_id'=>$user_info['openid'],'punch_activity.status'=>1,'user_punch_record.is_del'=>0];
            $public_result = $this->getPunchLists($where);

            //我创建的打卡
            $where = ['punch_activity.user_id'=>$user_info['openid'],'punch_activity.is_del'=>0];
            $my_punch = $this->getPunchLists($where);

            $result = array_merge($my_punch,$public_result);

            foreach ($result as &$value){
                $value['type'] = 1;
                if($value['user_id']==$user_info->openid){
                    $value['is_self'] = 1;
                }else{
                    $value['is_self'] = 0;
                }
                unset($value['user_id']);
                Redis::zadd('user_info'.$uid,strtotime($value['created_at']),json_encode($value));
            }
            //排序
            $results = array_merge($result,$share_info);
            foreach ($results as $k=>$v){
                $flag[$k] = $v['created_at'];
            }
            //进行排序 应该是同一个数组
            array_multisort($flag, SORT_DESC, $results);
            fun_respon(1,$results);
        }
    }


    /**
     * @param Request $request
     * 获取我的动态
     */
    public function getOtherDynamics(Request $request){
        $openid = $request->openid;
        $uid = $request->json('uid');
        if(!$uid) fun_respon(0,'缺少参数！');
        $user_info = user::find($uid);
        if(!($user_info)){
            fun_respon(0,'暂无数据!');
        }

        //查询用户的打卡列表
        $share_info = user_punch_record::leftjoin('user_share_info', "user_punch_record.punch_user_info_id", "=", "user_share_info.id")->leftJoin('punch_activity','user_punch_record.punch_ac_id','=','punch_activity.id')->leftJoin('user','user_share_info.user_id','=','user.openid')
            ->where(['user_punch_record.user_id'=>$user_info['openid'],'user_punch_record.is_valid'=>1])->orderBy('user_punch_record.created_at',"DESC")
            ->get(['share_image','user_share_info.content','address','punch_ac_id as punch_id','openid','nickName','avatarUrl','user_share_info.created_at','user_share_info.id as user_info_id','user_share_info.user_id as uid','punch_activity.title'])->toArray();
        if($share_info){
            foreach ($share_info as &$v){
                $v['type'] =2 ;
                //查询自己是否点赞
                $where = ['user_id'=>$openid,'user_share_info_id'=>$v['user_info_id'],'user_id'=>$openid,'status'=>1];
                $re = user_info_likes::where($where)->first();
                $v['is_like'] = $re?1:0;
                //查询点赞数量
                $where = ['user_share_info_id'=>$v['user_info_id'],'status'=>1];
                $like_num = user_info_likes::where($where)->count();
                $v['like_num'] = $like_num?$like_num:0;
                //评论列表
                $punch_comments = punch_comments::where(['share_info_id'=>$v['user_info_id']])->orderBy('created_at','ASC')->get();
                if($punch_comments){
                    foreach ($punch_comments as &$value){
                        if($value['to_user_id']){
                            $to_user_nickName = user::where(['id'=>$value['to_user_id']])->first(['nickName']);
                            $value['to_user_nickName'] = $to_user_nickName['nickName'];
                        }else{
                            $value['to_user_nickName'] = '';
                        }
                        $value['is_mine'] = $value['user_id']==$openid?1:0;
                        $user_nickName = user::where(['openid'=>$value['user_id']])->first(['nickName','id']);
                        $value['user_nickName'] = $user_nickName['nickName'];
                        $value['user_id'] = $user_nickName['id'];
                        unset($value['to_user_id']);
                        unset($value['share_info_id']);
                        unset($value['punch_id']);
                    }
                }
                $v['comments'] = $punch_comments;
            }

        }
        //我参与的公开打卡
        $where = ['user_punch_record.user_id'=>$user_info['openid'],'punch_activity.status'=>1,'user_punch_record.is_del'=>0];
        $public_result = $this->getPunchLists($where);

        //我创建的打卡
        $where = ['punch_activity.user_id'=>$user_info['openid'],'punch_activity.is_del'=>0];
        $my_punch = $this->getPunchLists($where);

        $result = array_merge($my_punch,$public_result);

        foreach ($result as &$value){
            $value['type'] = 1;
            if($value['user_id']==$user_info->openid){
                $value['is_self'] = 1;
            }else{
                $value['is_self'] = 0;
            }
            unset($value['user_id']);
        }
        $results = array_merge($result,$share_info);
        fun_respon(1,$results);
    }


    /**
     * @param $where
     * 格式化处理打卡数据
     */
    private function getPunchLists($where){
        $result = [];
        $punch = $this->getPunch($where);
        if($punch){
            $id = array_column($punch, 'punch_id');
            $result = array_combine($id, $punch);
        }
        return $result;
    }


    private function getUserShareInfos($where){
        $data = user_share_info::where($where)->get();
        return $data;
    }





    private function get_rand($proArr) {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);             //抽取随机数
            if ($randNum <= $proCur) {
                $result = $key;                         //得出结果
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }
}
